<?php

class Login extends CI_Controller {

    function __construct() {
        parent::__construct();
        $this->load->helper('cookie');
        $this->load->model("manage_model", "manage");
        $this->load->model("admin_model", "admin");

        $this->projectUrl = [
            "login" => MANAGE_URL . "&c=login",
            "ajaxLogin" => MANAGE_URL . "&c=login&m=ajaxLogin",
            "captcha" => MANAGE_URL . "&c=login&m=captcha",
        ];
        $this->indexUrl = [
            "index" => MANAGE_URL . "&c=index",
        ];
    }

    /*
     * 登录页面
     */

    function index() {

        $data = [];
        
        $data["cssFile"] = [
            "login" => [
                "src" => PUBLIC_URL . "/default/css/login.css",
            ],
        ];

        $custom_config = config_item("custom");
        $data["custom"] = $custom_config;

        $data["headTitle"] = "总后台管理-" . WEB_NAME;

        $this->layout->view('admin/login/index', $data);
    }

    /*
     * ajax登录
     */

    function ajaxLogin() {

        $flag = 0;
        $msg = "登录失败";
        $memberIndexUrl = "";

        $username = $this->input->postStr("username", TRUE);
        $password = $this->input->postStr("password", TRUE);
        $captchaNum = $this->input->postStr("vercode", TRUE);

        try {

            if ($username == "" || $password == "" || $captchaNum == "") {
                throw new Exception("必填项不能为空");
            } else {
                $encryptCaptcha = $this->common->encryptCaptcha($captchaNum);
                $captchaNumCookie = $this->input->cookie("captchaNum");
                if ($encryptCaptcha != $captchaNumCookie) {
                    throw new Exception("验证码错误");
                } else {
                    $password = $this->common->encryptPassword($password);
                    $result = $this->admin->checkAdminLogin($username, $password);
                    if (empty($result)) {
                        throw new Exception("用户名不存在或密码错误");
                    }
                    $this->admin->updateAdminInfo([
                        "fieldArr" => [
                            "login_time" => time(),
                        ],
                        "whereArr" => [
                            "id" => $result["id"],
                        ],
                    ]);
                    $sessionData = [
                        'session_admin_user_id' => $result['id'],
                        'session_admin_user_username' => $username,
                        'session_admin_user_is_login' => 1,
                    ];
                    $this->session->set_userdata($sessionData);
                    delete_cookie("captchaNum");
                    $flag = 1;
                }
            }
        } catch (Exception $e) {
            $msg = $e->getMessage();
        }

        $outArr = array("flag" => $flag, "msg" => $msg, "adminIndexUrl" => $this->indexUrl["index"]);
        echo jsonEncode($outArr);
    }

    /*
     * 退出
     */

    function logout() {
        $this->session->unset_userdata(['session_admin_user_id', 'session_admin_user_username', 'session_admin_user_is_login']);
        redirect($this->projectUrl["login"], 'refresh');
    }

    /*
     * 得到验证码
     */

    public function captcha() {

        //生成验证码图片 
        Header("Content-type: image/PNG");
        srand((double) microtime() * 1000000);
        $im = imagecreate(70, 20);
        $black = ImageColorAllocate($im, 0, 0, 0);
        $white = ImageColorAllocate($im, 255, 255, 255);
        $gray = ImageColorAllocate($im, 200, 200, 200);
        imagefill($im, 68, 30, $gray);

        $captchaNum = rand(1000, 9999);

        $this->input->set_cookie("captchaNum", $this->common->encryptCaptcha($captchaNum), time() + 3600);

        imagestring($im, 5, 10, 3, $captchaNum, $white);
        for ($i = 0; $i < 200; $i++) {   //加入干扰象素 
            $randcolor = ImageColorallocate($im, rand(0, 255), rand(0, 255), rand(0, 255));
            imagesetpixel($im, rand() % 70, rand() % 30, $randcolor);
        }

        ImagePNG($im);
        ImageDestroy($im);
    }

}
